<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addpermission(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" width="150" />
          <el-table-column label="头像" width="100" class="center">
            <template>
              <imgcomp class="img" :img="''" />

            </template>
          </el-table-column>
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button v-if="scope.row.type === 1" type="text" @click="addpermission(2,scope.row)">添加</el-button>
              <el-button type="text" @click="editpermission(scope.row)">编辑</el-button>
              <el-button type="text" @click="dropuser(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <el-dialog :visible.sync="showDialog" :title="tit" :close-on-click-modal="false" :close-on-press-escape="false" @close="close">
      <!-- 表单内容 -->
      <el-form ref="roleForm" label-width="100px" class="abc" :rules="rules" :model="showDialogformData">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="showDialogformData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="showDialogformData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="showDialogformData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="showDialogformData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="SubmitForm">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>

</template>

<script>
// getPermissionList 获取列表  addPermission 添加
import { getPermissionList, addPermission, delPermission, getPermissionDetail, updatePermission } from '@/api/permission'
import { tranListToTreeData } from '@/utils/tree'
export default {
  data() {
    const liebiaoname = (rule, value, callback) => {
      // console.log(this.nocodecf, value)
      this.liebiaocode.includes(value)
        ? callback(`权限标识 ${value}  已有`) : callback()
    }
    const liebiaocode = (rule, value, callback) => {
      // console.log(this.nocodecf, value)
      this.liebiaoname.includes(value)
        ? callback(`权限名称 ${value}  已有`) : callback()
    }
    return {
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: ['blur'] },
          { min: 2, max: 10, message: '权限名称要求1-50个字符', trigger: ['blur'] }, { validator: liebiaocode, trigger: ['blur'] }

        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: ['blur'] },
          { min: 2, max: 10, message: '权限标识要求1-50个字符', trigger: ['blur'] }, { validator: liebiaoname, trigger: ['blur'] }

        ]
      },
      liebiaocode: '',
      liebiaoname: '',
      tit: '', // 自定义title
      isEdit: false, // 是否是编辑项
      list: [], // 页面列表
      showDialog: false, // 是否显示弹层
      showDialogformData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      }
    }
  },
  created() {
    // 获取列表数据
    this.getPermissionList()
  },

  methods: {
    // 用户点击了添加
    // 这里业务背景是上面的添加是添加一整项，下面的添加是添加当前项的权限
    addpermission(type, row) {
      console.log(row)
      if (type === 1) {
        this.tit = '添加权限'
      } else {
        this.tit = ` 添加${row.name}`
      }
      this.isEdit = false
      // console.log(type, id)
      this.showDialog = true
      this.showDialogformData.type = type
      this.showDialogformData.pid = row.id
      // console.log('添加')

      // console.log(data)
    },

    // 点击编辑项

    async  editpermission(row) {
      this.tit = ` 编辑${row.name}`
      this.isEdit = true
      this.showDialog = true
      const data = await getPermissionDetail(row.id)

      this.showDialogformData = data.data
      this.showDialog = true
      // console.log('编辑')
    },
    // 提交添加
    async getAdd() {
      await addPermission(this.showDialogformData)
      this.$message.success('操作成功')
      this.getPermissionList()
      this.showDialog = false
    },
    // 提交编辑
    async  geteitd() {
      await updatePermission(this.showDialogformData)
      // 2. 更新数据

      this.getPermissionList()
      // 3. 提示
      this.$message.success('操作成功')
      // 4. 关闭弹层
      this.showDialog = false
    },
    // 点击删除用户事件
    dropuser(row) {
      this.$confirm('确定要删除该权限吗?', '温馨提示').then(async() => {
        // 1. 实现删除
        await delPermission(row.id)
        // 2. 重新加载数据
        this.getPermissionList()
        // 3. 提示
        this.$message.success('删除成功')
      }).catch(err => {
        this.$message.error(err.message)
      })
    },
    // 表单提交
    async  SubmitForm() {
      // 提交表单
      // console.log('提交')
      this.$refs.roleForm.validate(val => {
        if (val) {
          this.isEdit ? this.geteitd() : this.getAdd()
        }
      })
    },

    // 获取列表数据
    async getPermissionList() {
      const res = await getPermissionList()
      // console.log(res.data)
      // 获取全部的code来进行验证
      this.liebiaocode = res.data.map(res => {
        return res.code
      })
      this.liebiaoname = res.data.map(res => {
        return res.name
      })
      // console.log(this.liebiaoname)
      // console.log(this.liebiaocode)
      // console.log(res.data)
      // console.log(res.data)
      // this.list = res.data
      this.list = tranListToTreeData(res.data)
    },
    // 关闭弹层
    close() {
      this.showDialog = false
      console.log('关闭')
      this.showDialogformData = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 新增权限类型
        pid: '' // 添加到哪个节点下
      }
      this.$refs.roleForm.clearValidate()
      // this.clearValidate()
      // 弹出层关闭前回调
      // console.log('关闭')
    }
  }
}
</script>

<style  >
.img{
  width: 50px;
 margin: auto;
}
.center{
  text-align: center;

}

</style>
